

const app = getApp()
const env = app.globalData.env
const isLogin = app.globalData.isLogin
let user = app.globalData.user
let timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    intervFlag:0,
    flag: false,
    checkgroupData:[{
      value:"isAppoint",
      name:"待检查",
      checked:'true'
    },{
      value:"isChecked",
      name:"已完成",
      checked:'true'
    },]

  },

  bindGetUserInfo(e) {
    let _this = this
    console.log("binduserinfo", e)
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          console.log("19hang")
          wx.getUserProfile({
            desc: 'desc',
            success: (res) => {
              console.log(res)
              user['avatarUrl'] = res.userInfo.avatarUrl
              user['nickName'] = res.userInfo.nickName
              user['gender'] = res.userInfo.gender


              wx.setStorage({
                key: 'user',
                data: user
              })

              wx.request({
                // url: 'http://www.wantstong.com:88/api/user',
                url: env != 1 ? 'http://localhost:88/api/user' : 'https://www.wantstong.com/api/user',
                method: 'POST',
                data: {
                  data: user
                },
                success: res => {
                  app.globalData.isLogin = true
                  console.log(res.data.data[0])
                  wx.setStorage({
                    data: res.data.data[0],
                    key: 'user',
                  })
                  res.data.code == 200 ? wx.switchTab({
                    url: '/pages/index/index',
                  }) : wx.showToast({
                    title: 'error',
                  })
                  _this.setData({
                    flag: true
                  })
                  _this.getUser()
                }
              })


            }
          })
        } else {
          //用户没有授权
          console.log("用户没有授权");
        }
      }
    });



  },

  // 获取storage里面的user
  getUser() {
    console.log("getUser")
    let _this = this
    wx.getStorage({
      key: 'user',
      success: (res) => {
        console.log("index / getUser", res)
        _this.setData({
          user: res.data
        })
        _this.getQueue()
        
        wx.request({
          url: env != 1 ? 'http://localhost:88/api/searchAppointment' : 'https://www.wantstong.com/api/searchAppointment',
          method: "POST",
          data: {
           body: res.data,
           cmd:["isAppoint"]
          },
          success: (res) => {
            let num=res.data.data.length
            console.log("num",num)
            if(num!=0){
              wx.setTabBarBadge({
                index:0 ,
                text: num.toString(),
              })
            }else{
              wx.removeTabBarBadge({
                index: 0,
              })
            }
         
            console.log("查询预约", res)
            _this.setData({
              "expList": res.data.data
            })
          }
        })
      }
    })
  },


  getQueue() {
    let _this = this
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/queue' : 'https://www.wantstong.com/api/queue',
      method: 'POST',
      data: {
        data: _this.data.user
      },
      success: res => {
        console.log("138",res)
        let resarr=res.data.data
        let begin=0;
        for(let i=0;i<res.data.data.length;i++){
          console.log(_this.data.intervFlag)
      
          if(resarr[i]==0){
            begin=1;
            break
    
          }

         
          _this.setData({
            dataQueue: res.data.data
          })
      
          
        }
        if(_this.data.intervFlag==0){
          wx.vibrateLong({
            type:"heavy",
            success: (res) => {
              console.log(res)
            },
          })
      
      }
        _this.setData({
          dataQueue: res.data.data
        })
      }
    })
  },

  checkboxChange(e){
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    let cmd=e.detail.value
    wx.request({
      url: 'https://www.wantstong.com/api/searchAddition',
      method:"POST",
      data:{
        cmd:cmd,
        user:this.data.user
      },
      success:res=>{
        this.setData({
          expList:res.data.data
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */


  onLoad: function (options) {
    this.getUser()
    wx.getStorage({
      key: 'orderBadge',
      success:res=>{
        console.log("71",res)
        let num=res.data.toString()
        if(res.data!=0){
          wx.setTabBarBadge({
            index: 1,
            text: num,
          })
        }
      
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showModal({
      title:"tips",
      content:"下拉取消震动哦！"
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this=this
    setInterval(function(){
      _this.getUser()
    }
     
    ,10000) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉")

    this.setData({
      intervFlag:1
    })
    clearInterval(timer)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})